import * as Cesium from 'cesium';
import * as Libora from '../../public/libora/index.js'

(window).Cesium = Cesium;
(window).Libora = Libora;

export function centerAt(options) {
  const center = Cesium.Cartesian3.fromDegrees(options.x, options.y);
  const heading = Cesium.Math.toRadians(options.heading);
  const pitch = Cesium.Math.toRadians(options.pitch);
  const range = options.z;
  options.viewer.camera.flyToBoundingSphere(new Cesium.BoundingSphere(center, 0),
    {
      offset: new Cesium.HeadingPitchRange(heading, pitch, range),
      duration: options.duration !== undefined ? options.duration : 1,
    },
  );
}

export default class Demo {
  constructor() {
    const viewer = Libora.getViewer({});
    this._waterManager = new Libora.WaterManager(viewer);

    this.addWater("../../public/json/hedao_nei.json");
    this.addWater("../../public/json/hedao_wai.json");
    centerAt({
      viewer,
      "x": 121.480207,
      "y": 29.792675,
      "z": 190.8,
      "heading": 196.1,
      "pitch": -49,
      "roll": 359.9
    });
  }

  addWater(url) {
    var promise = Cesium.GeoJsonDataSource.load(url);
    promise.then(dataSource => {
      this._waterManager.addWater(dataSource.entities.values, 4)
    }).catch(error => {
      console.error(error);
    });
  }
}
